package com.xxkk.logic;

import com.ebo.synframework.synroom.executor.pool.SynRoomThreadPool;
import com.xxkk.net.websocketNetty.WebSocketServerPipelineFactory;
import com.xxkk.gameCore.global.globalServer.ServerData;
import org.apache.log4j.Logger;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.springframework.context.ApplicationContext;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

public class LogicBoot {
    public static final Logger logger = Logger.getLogger("BootStart");

    public static void start(ApplicationContext ac) {
        try {
            init();
            startSocketServer(ac);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static void startSocketServer(ApplicationContext ac) {
        ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
        SynRoomThreadPool synRoomThreadPool = new SynRoomThreadPool(5, 10, 40, 10000, "synRoomThreadPool");
        LogicMsgHandler logicMsgHandler = ac.getBean(LogicMsgHandler.class);
        logicMsgHandler.setApplicationContext(ac);
        logicMsgHandler.setSynRoomThreadPool(synRoomThreadPool);
        if (true) {
            WebSocketServerPipelineFactory pipelineFactory = new WebSocketServerPipelineFactory(logicMsgHandler);
            bootstrap.setPipelineFactory(pipelineFactory);
        }
        bootstrap.setOption("child.tcpNoDelay", true);
        bootstrap.setOption("child.receiveBufferSize", 1048576);// 1048576
        ServerData serverData = ac.getBean(ServerData.class);
        int port = serverData.getSelfServerInfo().getWebSocketPort();
        InetSocketAddress address = new InetSocketAddress("0.0.0.0", port);
        Channel channel = bootstrap.bind(address);
        logger.info("##logicBoot start!" + address.toString() + "#" + channel.toString());
    }

    private static void init() {

    }

}
